/**
 * 角色管理JS
 * @author Assassin
 */

// 添加角色
function executeRoleAdd() {
    // 表单验证
    var roleName = $.trim($('#roleName').val());
    if(shiro.util.isEmpty(roleName)) {
        layer.tips('角色名称不能为空', '#roleName');
        return false;
    }
    var treeObj = $.fn.zTree.getZTreeObj("menuTree");
  	var nodes = treeObj.getCheckedNodes(true);
  	if(nodes.length==0){
  		 layer.msg("请选择角色菜单", {icon: 5,time: 1000});
  		 return false;
  	}else{
  		var menuIds = "";
      	for (var i = 0; i < nodes.length; i++) {
      		menuIds += nodes[i].id + ",";
      	}  	
      	$('#menuIds').val(menuIds);
  	}
    // 提交添加
    shiro.ajax({
        url: '/role/executeAddRole',
        data:$('#roleAddForm').serialize(),
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "role/list";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
                $('#roleName').val('');
            }
        }
    });
    return false;
}

// 修改角色
function executeRoleEdit() {
    // 表单验证
    var roleName = $.trim($('#roleName').val());
    if(shiro.util.isEmpty(roleName)) {
        layer.tips('角色名称不能为空', '#roleName');
        return false;
    }
    var treeObj = $.fn.zTree.getZTreeObj("menuTree");
  	var nodes = treeObj.getCheckedNodes(true);
  	if(nodes.length==0){
  		 layer.msg("请选择角色菜单", {icon: 5,time: 1000});
  		return false;
  	}else{
  		var menuIds = "";
      	for (var i = 0; i < nodes.length; i++) {
      		menuIds += nodes[i].id + ",";
      	}  	
      	$('#menuIds').val(menuIds);
  	}
    // 提交修改
    shiro.ajax({
        url: 'role/executeEditRole',
        data:$('#userRoleForm').serialize(),
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "role/list";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
                $('#realName').val('');
            }
        }
    });
    return false;
}

// 删除角色
function deletetRole(roleId) {
    layer.msg('确定删除该角色？', {
        time: 0,
        btn: ['确定', '取消'],
        yes: function(index){
            layer.close(index);
            // 提交删除
            shiro.ajax({
                url: 'role/delete',
                data:{"roleId": roleId},
                success: function(data) {
                    if(data.result == 0) {
                        layer.msg(data.message, {icon: 1,time: 1000}, function() {
                            window.location.href = shiro.baseUrl + "role/list";
                        });
                    } else {
                        layer.msg(data.message, {icon: 5,time: 1000});
                    }
                }
            });
        }
    });
}

// 批量删除角色
function batchDeleteRoles() {
    var roleIdArray = shiro.form.getCheckBoxSelectValues('roleIdCheckBox');
    if(roleIdArray.length == 0) {
        layer.msg('请选择要操作的数据行', {icon: 5,time: 1000});
    } else {
        layer.msg('确定删除选中的角色？', {
            time: 0,
            btn: ['确定', '取消'],
            yes: function(index){
                layer.close(index);
                var roleIds = roleIdArray.join(',');
                // 提交删除
                shiro.ajax({
                    url: 'role/batchDelete',
                    data:{"roleIds": roleIds},
                    success: function(data) {
                        if(data.result == 0) {
                            layer.msg(data.message, {icon: 1,time: 1000}, function() {
                                window.location.href = shiro.baseUrl + "role/list";
                            });
                        } else {
                            layer.msg(data.message, {icon: 5,time: 1000});
                        }
                    }
                });
            }
        });
    }
}